package com.ruoyi.web.controller.fenxi;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ROrgRisk;
import com.ruoyi.system.service.IROrgRiskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 风险统计标Controller
 * 
 * @author ruoyi
 * @date 2022-09-25
 */
@Controller
@RequestMapping("/system/risk")
public class ROrgRiskController extends BaseController
{
    private String prefix = "system/risk";

    @Autowired
    private IROrgRiskService rOrgRiskService;

//    @RequiresPermissions("system:risk:view")
    @GetMapping()
    public String risk()
    {
        return prefix + "/risk";
    }

    /**
     * 查询风险统计标列表
     */
//    @RequiresPermissions("system:risk:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ROrgRisk rOrgRisk)
    {
        startPage();
        List<ROrgRisk> list = rOrgRiskService.selectROrgRiskList(rOrgRisk);
        return getDataTable(list);
    }

    @GetMapping ("/list")
    @ResponseBody
    public TableDataInfo glist(ROrgRisk rOrgRisk)
    {
        startPage(1,20);
        List<ROrgRisk> list = (List<ROrgRisk>) CacheUtils.get(CacheUtils.PIE_KEY);
        if(Objects.isNull(list)||list.isEmpty()||Objects.nonNull(rOrgRisk.getLev()))     {
            list = rOrgRiskService.selectROrgRiskList(rOrgRisk);
//            CacheUtils.put(CacheUtils.PIE_KEY,list);
        }

        return getDataTable(list);
    }

    /**
     * 导出风险统计标列表
     */
//    @RequiresPermissions("system:risk:export")
    @Log(title = "风险统计标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ROrgRisk rOrgRisk)
    {
        List<ROrgRisk> list = rOrgRiskService.selectROrgRiskList(rOrgRisk);
        ExcelUtil<ROrgRisk> util = new ExcelUtil<ROrgRisk>(ROrgRisk.class);
        return util.exportExcel(list, "风险统计标数据");
    }

    /**
     * 新增风险统计标
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存风险统计标
     */
//    @RequiresPermissions("system:risk:add")
    @Log(title = "风险统计标", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ROrgRisk rOrgRisk)
    {
        return toAjax(rOrgRiskService.insertROrgRisk(rOrgRisk));
    }

    /**
     * 修改风险统计标
     */
//    @RequiresPermissions("system:risk:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ROrgRisk rOrgRisk = rOrgRiskService.selectROrgRiskById(id);
        mmap.put("rOrgRisk", rOrgRisk);
        return prefix + "/edit";
    }

    /**
     * 修改保存风险统计标
     */
//    @RequiresPermissions("system:risk:edit")
    @Log(title = "风险统计标", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ROrgRisk rOrgRisk)
    {
        return toAjax(rOrgRiskService.updateROrgRisk(rOrgRisk));
    }

    /**
     * 删除风险统计标
     */
//    @RequiresPermissions("system:risk:remove")
    @Log(title = "风险统计标", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(rOrgRiskService.deleteROrgRiskByIds(ids));
    }
}
